import {createApp} from 'vue'
import App from './App.vue'

import router from "./router"; //导入router
import axios from "axios";

axios.defaults.baseURL = 'http://localhost:8080'  //服务器基准URL地址
axios.interceptors.request.use(
    config => {

        //将token携带进header头
        const token = sessionStorage.getItem("adminToken");
        if (token) {
            config.headers['token'] = token;
        }

        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
axios.interceptors.response.use(
    response => {
        if (response.data.code == 401){
            sessionStorage.removeItem("adminToken")
            sessionStorage.removeItem("user")
            ElMessage.error(response.data.msg)
            router.push({
                path:'/login'
            })
        }
        return response
    },
    error => {
        console.log('err' + error) //for debug
        return Promise.reject(error)
    }
);
/* 引入Element-Plus插件 */
import ElementPlus, {ElMessage} from 'element-plus'
/* 引入Element-Plus样式 */
import 'element-plus/dist/index.css'
/*解决Element-plus默认分页文字为英文*/
// @ts-ignore
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App) //创建vue实例对象
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.use(router) //使用router
/* 使用Element-Plus插件 */
app.use(ElementPlus, {
    locale: zhCn,
})
app.mount('#app')
